{layout name="common/base" }
<table class="layui-hide" id="currentTableId" lay-filter="tooFilter"></table>

<script type="text/html" id="user">
    {{ d.user.nick_name }}
    <img width="34px" height="34px" src="{{d.user.avatarfull}}">
</script>
<script type="text/html" id="name">
    {{ d.name }} / {{ d.mobile }}
</script>

<script type="text/html" id="switchStatus">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="switchStatus" {{ d.status == 0 ? 'checked' : '' }}>
</script>

<script type="text/html" id="currentTableBar">
    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete delete" lay-event="delete" data-id="{d.id}">删除</a>
</script>

<script>
    layui.use(['form', 'table', 'util'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            util = layui.util;
        table.render({
            elem: '#currentTableId',
            url: '/admin/dealer.user/lstajax',
            toolbar: '#toolbarHead',
            cols: [[
                {field: 'id', width: 100, title: 'ID'},
                {field: 'user', width: 200, title: '会员信息', templet: '#user'},
                {field: 'name', width: 200, title: '姓名/手机号', templet: '#name'},
                {field: 'money', width: 200, title: '累计佣金/可提现佣金'},
                {field: 'referee_id', width: 200, title: '推荐人'},
                {field: 'first_num', width: 200, title: '下级用户'},
                {field: 'status', width: 100, title: '状态', templet: '#switchStatus', unresize: true},
                {field: 'create_time', width: 200, title: '创建时间', sort: true},
                {title: '操作', width: 150, templet: '#currentTableBar', fixed: "right", align: "center"}
            ]],
            limit: 10,
            page:{
                elem: 'page',
                layout: ['prev', 'page', 'next', 'last','skip'], //自定义分页布局
                groups: 5, //只显示 5 个连续页码
                prev: '上一页',
                next: '下一页',
                first: 0 //不显示首页
            },
            loading:true

        });

        table.on('tool(tooFilter)', function(obj){
            var data = obj.data;
            console.log(data);
            switch(obj.event){

                case 'delete':
                    layer.confirm('真的删除么?',{icon:3, title:'提示'}, function (index) {
                        $.ajax({
                            url: '/admin/dealer.user/del/id/' + data.id,
                            type: 'GET',
                            success: function(res) {
                                //window.location.reload();
                                if(res.code == 1){
                                    layer.msg(res.msg, {
                                        icon: 1,
                                        time: 1000 //2秒关闭（如果不配置，默认是3秒）
                                    }, function(){
                                        obj.del();
                                        layer.close(index);
                                    });
                                }else{
                                    layer.msg(res.msg, {
                                        icon: 1,
                                        time: 1000 //2秒关闭（如果不配置，默认是3秒）
                                    });
                                }
                            },
                            error: function(res) {
                                //return   err(res)
                            }
                        });
                    });
                    break;
                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            }
        });

        form.on('switch(switchStatus)', function(obj){
            var id = this.value;
            var status = obj.elem.checked ? 0 : 1;
            console.log(id);
            console.log(status);
            $.ajax({
                url: '{:url("dealer.user/switchStatus")}?id=' + id + '&status=' + status,
                success: function(res){
                    layer.msg(res.msg);
                }
            });
            return false;
        });

    });

</script>